// ConfigMgr.cpp: implementation of the CConfigMgr class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "CommonDef.h"
#include "CommonFunc.h"
#include "ConfigMgr.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CConfigMgr::CConfigMgr()
{

}

CConfigMgr::~CConfigMgr()
{

}


BOOL CConfigMgr::LoadFtpConfig(LPFTPDATA pFtpData)
{
	CString strConfigPath;
	GetConfigFilePath(strConfigPath);
	if (!::PathFileExists(strConfigPath))
	{
		return FALSE;
	}
	static struct{
		LPCTSTR szKeyname;
		LPTSTR szValue;
	}
	Keynames[] = {
		{L"server", pFtpData->szServer},
		{L"username", pFtpData->szUser},
		{L"password", pFtpData->szPwd},
		{L"defaultpath", pFtpData->szDefaultPath}
	};

	::GetPrivateProfileString(APP_NAME_FTP, Keynames[0].szKeyname, NULL, Keynames[0].szValue,  MAX_PATH, strConfigPath);
	::GetPrivateProfileString(APP_NAME_FTP, Keynames[1].szKeyname, NULL, Keynames[1].szValue,  MAX_USER_PWD, strConfigPath);
	::GetPrivateProfileString(APP_NAME_FTP, Keynames[2].szKeyname, NULL, Keynames[2].szValue,  MAX_USER_PWD, strConfigPath);
	::GetPrivateProfileString(APP_NAME_FTP, Keynames[3].szKeyname, NULL, Keynames[3].szValue,  MAX_PATH, strConfigPath);
	return TRUE;
}

BOOL CConfigMgr::LoadDownloadConfig(LPDOWNLOADDATA pDownloadData)
{
	CString strConfigPath;
	GetConfigFilePath(strConfigPath);
	if (!::PathFileExists(strConfigPath))
	{
		return FALSE;
	}

	TCHAR szFileType[MAX_PATH] = {0};
	TCHAR szTime[MAX_PATH] = {0};

	static struct{
		LPCTSTR szKeyname;
		LPTSTR szValue;
	}
	Keynames[] = {
		{L"remotepath", pDownloadData->szRemotePath},
		{L"localpath", pDownloadData->szLocolPath},
		{L"filetype", szFileType},
		{L"fromwhen", szTime},
	};
	
	::GetPrivateProfileString(APP_NAME_DOWNLOAD, Keynames[0].szKeyname, NULL, Keynames[0].szValue,  MAX_PATH, strConfigPath);
	::GetPrivateProfileString(APP_NAME_DOWNLOAD, Keynames[1].szKeyname, NULL, Keynames[1].szValue,  MAX_PATH, strConfigPath);
	::GetPrivateProfileString(APP_NAME_DOWNLOAD, Keynames[2].szKeyname, NULL, Keynames[2].szValue,  MAX_PATH, strConfigPath);
	::GetPrivateProfileString(APP_NAME_DOWNLOAD, Keynames[3].szKeyname, NULL, Keynames[3].szValue,  MAX_PATH, strConfigPath);

	pDownloadData->dwFileType = ParseFileType(szFileType);
	ParseTime(szTime, (pDownloadData->tTimeRestrict));

	return TRUE;
}

DWORD CConfigMgr::ParseFileType(const CString & strFileType)
{
	DWORD dwFileType = 0;
	if (strFileType.IsEmpty())
	{
		return dwFileType;
	}

	CString strType = strFileType;
	strType.TrimRight();
	strType.TrimRight(L",");


	static struct {
		LPCTSTR szName;
		DWORD dwType;
	}
	NameType[FILE_TYPE_COUNT] = {
		{L"zip", FILE_TYPE_ZIP},
		{L"7z", FILE_TYPE_7Z},
	};


	int nPos = 0;
	int nStart = 0;
	do 
	{
		nPos = strType.Find(L",", nStart);
		CString strTemp = strType.Mid(nStart);
		if (nPos > 0)
		{
			strTemp	= strType.Mid(nStart, nPos);
		}

		for (int i = 0; i < FILE_TYPE_COUNT; ++i)
		{
			if (!strTemp.CompareNoCase(NameType[i].szName))
			{
				dwFileType |= NameType[i].dwType;
			}
		}

		nStart = nPos + 1;
	} while (nPos > 0);

	return dwFileType;
}

BOOL CConfigMgr::ParseTime(const CString & strTime, SYSTEMTIME & time)
{
	DWORD dwFileType = 0;
	if (strTime.IsEmpty())
	{
		return FALSE;
	}
		
	swscanf(strTime, L"%d-%d-%d-%d-%d-%d", &time.wYear, &time.wMonth, &time.wDay, &time.wHour, &time.wMinute, &time.wSecond);

	return TRUE;
}